#-*- encoding=utf-8 -*-
'''
Description: 
version: 1.0.0
Author: peng2.zhang
Date: 2021-05-12 10:20:46
LastEditors: peng2.zhang
LastEditTime: 2021-11-24 16:30:36
'''
import time
import ujson
from ci_conclude.models.db import DBConnection
from ci_conclude.models.models import WxUser
from ci_conclude.services.base import BaseService
from ci_conclude.utils.error_code import ERROR
from ci_conclude.utils.validations import Validator, validate
from voluptuous import Schema, ALLOW_EXTRA, Required, All, Any, Coerce, Length
from ci_conclude.log import logger
# from celery_app import task_deploy
# from celery_app.base import DBConnection


class WxUserService(BaseService,Validator):

    def get_list(self):
        validate_schema = Schema({
            Required("username_list", msg=u"username_list can not be null"): All(
                Coerce(list, msg="username error")
            ),
        }, extra=ALLOW_EXTRA)
        validate_result = self.check_schema(validate_schema)
        schema = validate_result.data
        username_list = schema.get("username_list")
        all = self.db.query(WxUser).filter(WxUser.username.in_(username_list)).all()
        datas = {i.as_dict()["username"]: i.as_dict()["mobile"] for i in all}
        return self.success(data=datas)
        # if first:
        #     return self.success(data=first.as_dict())
        # else:
        #     return self.failure(ERROR.database_get_err)
    
    @classmethod
    def get_mobiles(cls, username_list):
        args = {"username_list": username_list}
        with DBConnection() as db_conn:
            svc = cls(db_conn,args)
            res = svc.get_list()
            logger.info(res)
        mobile_list = [res["data"].get(x, "") for x in username_list]
        return mobile_list
        # if res["code"] == 0:
        #     return res["data"]["mobile"]
        # else:
        #     return ""
